<?php
include_once ('auth.php');
include_once ('online.php');
include_once ('sgbd.php');

if (! authenticate(true)) {
	header ("location: index.php");
	exit(1);
}
?>

<?php
	$max_lines = 100;
	$sort_col = 0;
	$order = "asc";
	$file_name = "liste.php";
	$table = "liste";
//	$head_color = 'orange';
	$sql_color = 'green';
	$search = "";
	$pattern = "";
	$head = array("id","Pays","D&eacute;partement","Nom","Type d'aire","Latitude1","Latitude2","Longitude1","Longitude2","Tarif","Type borne","Eau potable","Branchement &eacute;lectrique","Vidange WC","WC publics","Vidange eaux us&eacute;es");

	function set_params () {
		global $max_lines;
		global $sort_col; 
		global $order; 
		global $file_name; 
		global $table; 
		global $head;
		global $search;
		global $pattern;
		
		if (isset($_GET['max_lines']))
			$max_lines = $_GET['max_lines'];
			
		if (isset($_GET['sort_col']))
			$sort_col = $_GET['sort_col'];			

		if (isset($_GET['order']))
			$order = $_GET['order'];
			
		if (isset($_GET['search']))
			$search = $_GET['search'];			
	
		if (isset($_GET['pattern']))
			$pattern = $_GET['pattern'];			
	}
	
	function load_list () {
		global $max_lines;
		global $sort_col;
		global $order;
		global $table;
		global $sql_color;
		global $search;
		global $pattern;
		
		$sql = "select * from $table ".(strlen($search) > 0 ? "where $search like '%$pattern%'" : "")." order by ".($sort_col+1)." $order limit 0, $max_lines";
		echo "<font color='$sql_color'><h6>$sql</h6></font><br />\n";
		$res = query($sql);
		
		return $res;
	}
	
	set_params ();
	
	$list = load_list ();
?>

<html>

<form method='get' action="<?php echo $file_name;?>">
	Nombre de resultats &agrave; afficher: <input type="text" name="max_lines" /><input type='submit' value='Go' /><br />
	(0 pour tout afficher) <br />
	<input type='hidden' name='order' value="<?php echo $order; ?>" />
	<input type='hidden' name='sort_col' value="<?php echo $sort_col; ?>" />
	<input type='hidden' name='search' value="<?php echo $search; ?>" />
	<input type='hidden' name='pattern' value="<?php echo $pattern; ?>" />
</form>

<form method='get' action="<?php echo $file_name;?>">
	<select name='search'>
<?php
		foreach ($head as $h) {
			echo "<option>$h</option>";
		}
?>
	</select>
	<input type='text' name='pattern' />
	<input type='submit' value='Rechercher' />
	<input type='hidden' name='max_lines' value="<?php echo $max_lines; ?>" />
	<input type='hidden' name='order' value="<?php echo $order; ?>" />
	<input type='hidden' name='sort_col' value="<?php echo $sort_col; ?>" />
</form>

<br />
<br />

<table border='1'>
	<tr><b>
<?php
		$i = 0;
		
		foreach ($head as $h) {
			if ($sort_col == $i)
				echo "<td><a href='$file_name?order=desc&sort_col=$i&max_lines=$max_lines&search=$search&pattern=$pattern'><b>$h</b></a></td>";
			else
				echo "<td><a href='$file_name?order=asc&sort_col=$i&max_lines=$max_lines&search=$search&pattern=$pattern'>$h</a></td>";

			$i++;
		}
?>
	</b></tr>
	
<?php
	while ($row = mysql_fetch_row($list)) {
		echo "<tr>";
	
		foreach ($row as $cell) {
			echo "<td>".$cell."</td>";
		}
	
		echo "</tr>\n";
	}
	
	close ();
?>
</table>
</html>
